<?php
/**
 * @title 64. 最小路径和
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function minPathSum($grid) {
        /**
         * @since 2024-01-02 行数和列数
         */
        $row = count($grid);
        $col = count($grid[0]);

        /**
         * @since 2024-01-02 新建一个移动到每个点的路径，数字总和最小
         */
        $dp = array_fill(0, $row, array_fill(0, $col, PHP_INT_MAX));
        $dp[0][0] = $grid[0][0];

        /**
         * @since 2024-01-02 广度遍历
         */
        $nodeArray = [[0,0]];
        while($nodeArray){
            /**
             * @since 2024-01-02 下次遍历的点集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-02 遍历当前点
             */
            foreach ($nodeArray as list($r, $c)){
                /**
                 * @since 2024-01-02 每次只能向下或者向右移动一步
                 */
                $moveArray = [
                    [$r+1, $c],
                    [$r, $c+1],
                    // [$r-1, $c],
                    // [$r, $c-1],
                ];

                /**
                 * @since 2024-01-02 遍历
                 */
                foreach ($moveArray as list($nextR, $nextC)){
                    if(isset($grid[$nextR][$nextC])){
                        /**
                         * @since 2024-01-02 下一个点的数字总和
                         */
                        $nextSum = $dp[$r][$c] + $grid[$nextR][$nextC];

                        /**
                         * @since 2024-01-02 如果下一个点的数字总和小于当前点的数字总和
                         */
                        if($nextSum < $dp[$nextR][$nextC]){
                            /**
                             * @since 2024-01-02 更新下一个点的数字总和
                             */
                            $dp[$nextR][$nextC] = $nextSum;

                            /**
                             * @since 2024-01-02 下次遍历的点集合，键值防止重复
                             */
                            $nextNodeArray["{$nextR}-{$nextC}"] = [$nextR, $nextC];
                        } else {}
                    } else {}
                }
            }

            /**
             * @since 2024-01-02 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return Integer
         */
        return $dp[$row-1][$col-1];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.02_5.php
 */
$datas = [
    // [[[1,3,1],[1,5,1],[4,2,1]]], // 7
    [[[5,4,2,9,6,0,3,5,1,4,9,8,4,9,7,5,1],[3,4,9,2,9,9,0,9,7,9,4,7,8,4,4,5,8],[6,1,8,9,8,0,3,7,0,9,8,7,4,9,2,0,1],[4,0,0,5,1,7,4,7,6,4,1,0,1,0,6,2,8],[7,2,0,2,9,3,4,7,0,8,9,5,9,0,1,1,0],[8,2,9,4,9,7,9,3,7,0,3,6,5,3,5,9,6],[8,9,9,2,6,1,2,5,8,3,7,0,4,9,8,8,8],[5,8,5,4,1,5,6,6,3,3,1,8,3,9,6,4,8],[0,2,2,3,0,2,6,7,2,3,7,3,1,5,8,1,3],[4,4,0,2,0,3,8,4,1,3,3,0,7,4,2,9,8],[5,9,0,4,7,5,7,6,0,8,3,0,0,6,6,6,8],[0,7,1,8,3,5,1,8,7,0,2,9,2,2,7,1,5],[1,0,0,0,6,2,0,0,2,2,8,0,9,7,0,8,0],[1,1,7,2,9,6,5,4,8,7,8,5,0,3,8,1,5],[8,9,7,8,1,1,3,0,1,2,9,4,0,1,5,3,1],[9,2,7,4,8,7,3,9,2,4,2,2,7,8,2,6,7],[3,8,1,6,0,4,8,9,8,0,2,5,3,5,5,7,5],[1,8,2,5,7,7,1,9,9,8,9,2,4,9,5,4,0],[3,4,4,1,5,3,3,8,8,6,3,5,3,8,7,1,3]]], // 82
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minPathSum(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
